<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class UserShowForm extends CMiniyunStatForm
{
	public $data;
	public $id;
	public function __construct($id){
		$this->id = $id;
		$this->data = new UserFull();
		$this->data->initData($id);
			
	}
	public function initStatData(){
				$this->model = new UserFile();
		parent::initData();		$this->data->fillFileInfo($this->id,$this->startDate,$this->endDate);
		$this->data->fillDeviceInfo($this->id,$this->startDate,$this->endDate);
	}
	protected   function getInitDataSql()
	{
		return "SELECT DATE(max(created_at)) as endDate,DATE(min(created_at)) as startDate FROM `".$this->model->tableName()."` where user_id=".$this->id;
	}
	
	public function statChartPie(){
	 $this->data->usedSpace = 0;
	 $dbCommand = Yii::app()->db->createCommand("SELECT sum(file_size)/(1024*1024) as usedSpace,user_id  FROM `".Yii::app()->params['tablePrefix']."files` where created_at>='".$this->startDate." 00:00:00' and created_at<='".$this->endDate." 23:59:59' and user_id=".(int)$this->id);
	 $data =  $dbCommand->queryAll();
	 foreach($data as $index=>$item){
	 	$this->data->usedSpace = $item["usedSpace"];
	 }
	 	 	 $charOptions = array(
	  'theme' => 'gray',
	  'title' => array('text' => '使用统计'),
      'tooltip' => array(
			   'formatter' => 'js:function(){ return \'<b>\'+ this.point.name +\'</b>: \'+ this.y +\' M ，占\'+ForDight(this.percentage,1) +\' %\';}' 
			   ),
      'series' => array( 
			   array(
         'type' => 'pie',
         'data' => array(
			   array('已经使用',(int)$this->data->usedSpace),
			   array('尚未使用',(int)($this->data->space-$this->data->usedSpace)),
			   ),
			   )
			   )
			   );
			   return $charOptions;

	}
	
	public function statHistogram(){
		$space= 10;		$s = strtotime($this->startDate);
		$e = strtotime($this->endDate);
		$diff = (int)($e-$s)/(24*3600);
		if($diff>$space){
			$spaceValue = (int)$diff/$space;		}else{
			$spaceValue = 1;		}
		$x = array();
		for($item=$s; $item<=$e; $item+=$spaceValue*24*3600){
			array_push($x,date("Y-m-d",$item));
		}
		$data = array();		foreach($x as $index=>$currentDate){
			$data[$currentDate]=(int)($this->model->count("user_id=? and created_at<='".$currentDate." 23:59:59'",array($this->id)));
		}
		$charOptions = array(
		'theme' => 'gray',
      'title' => array('text' => '文件增长直方图'),
      'xAxis' => array(
         'categories' => array_keys($data)
		),
      'yAxis' => array(
         'title' => array('text' => '文件')
		),
      'series' => array(
		array('name' => '当前文件数', 'data' => array_values($data)),
		)
		);
		return $charOptions;
	}
}